package com.jing.mvp_demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jing.mvp_demo.Presenter.LoginPresenter;
import com.jing.mvp_demo.Presenter.RegisterPresenter;
import com.jing.mvp_demo.View.LoginView;
import com.jing.mvp_demo.View.RegisterView;

public class MainActivity extends AppCompatActivity implements LoginView , RegisterView {

    private EditText username;
    private EditText password;
    private Button loginButton,registerButton;
    private LoginPresenter loginPresenter;
    private RegisterPresenter registerPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        loginButton = findViewById(R.id.login_button);
        registerButton = findViewById(R.id.register_button);

        loginPresenter = new LoginPresenter(this);
        registerPresenter = new RegisterPresenter(this);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginPresenter.validateCredentials(username.getText().toString(), password.getText().toString());
            }
        });
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username_str = username.getText().toString();
                String password_str = password.getText().toString();
                // 确认密码可以根据需要处理

                registerPresenter.register(username_str, password_str);
            }
        });
    }


    @Override
    public void setUsernameError() {
        username.setError(getString(R.string.username_error));
    }

    @Override
    public void setPasswordError() {
        password.setError(getString(R.string.password_error));
    }

    @Override
    public void showRegistrationSuccessfulMessage() {
        Toast.makeText(this, "Register Successful", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void navigateToHome() {
        Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show();
    }
}